package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.StringUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.teacher.R;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.mvp.IView;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.umeng.analytics.MobclickAgent;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-18 14:48:49
 */
@Route(path = RouterHub.APP_JOINCLASSACTIVITY)
public class JoinClassActivity extends BaseActivity implements IView {

    private static final int REQUEST_CODE_SCAN = 113;
    @BindView(R.id.et_baseno)
    EditText etBaseno;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @Autowired(name = Intent.EXTRA_TITLE)
    String title;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_join_class;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (title.equals(Constants.JOIN_CLASS)) {
            MobclickAgent.onEvent(getActivity(),"ttw_join_class");
            etBaseno.setHint("请输入班级号");
            tvTip.setVisibility(View.VISIBLE);
        } else {
            tvTip.setVisibility(View.GONE);
            etBaseno.setHint("请输入好友慧学号");
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    public Activity getActivity() {
        return this;
    }

    @OnClick({R.id.btn_search, R.id.layout_scan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_search:
                String baseno = etBaseno.getText().toString();
                if (StringUtils.isTrimEmpty(baseno)) {
                    RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, title.equals(Constants.JOIN_CLASS)?"请输入班级号!":"请输入慧学号", 1500);
                    return;
                }
                if (title.equals(Constants.JOIN_CLASS)) {
                    ARouter.getInstance().build(RouterHub.APP_CLASSDETAILSACTIVITY)
                            .withString(Intent.EXTRA_TITLE, Constants.CLASS_DETAILS)
                            .withString(Constants.ROUTER_STRING, RxUtil.getClassCode(baseno))
                            .withBoolean(Constants.ROUTER_BOOLEAN, true)
                            .navigation();
                }else{
                    ARouter.getInstance().build(RouterHub.APP_CONTACTDETAILSACTIVITY)
                            .withString(Intent.EXTRA_TITLE, Constants.CONTACT_DETAILS)
                            .withString(Constants.ROUTER_STRING, RxUtil.getUserCode(baseno))
                            .navigation();
                }
                break;
            case R.id.layout_scan:
                Intent intent = new Intent(getActivity(), CaptureActivity.class);
                ZxingConfig config = new ZxingConfig();
                config.setPlayBeep(true);//是否播放扫描声音 默认为true
                config.setShake(true);//是否震动  默认为true
                config.setDecodeBarCode(false);//是否扫描条形码 默认为true
                config.setReactColor(R.color.white);//设置扫描框四个角的颜色 默认为淡蓝色
                config.setFrameLineColor(R.color.white);//设置扫描框边框颜色 默认无色
                config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
                intent.putExtra(com.yzq.zxinglibrary.common.Constant.INTENT_ZXING_CONFIG, config);
                startActivityForResult(intent, REQUEST_CODE_SCAN);
                break;
            default:
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 扫描二维码/条码回传
        if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
            if (data != null) {
                String code = data.getStringExtra(Constant.CODED_CONTENT);
                if (code.startsWith(Constants.QRCODE_PREFIX_TYPE.CLASS)) {
                    ARouter.getInstance().build(RouterHub.APP_CLASSDETAILSACTIVITY)
                            .withString(Intent.EXTRA_TITLE, Constants.CLASS_DETAILS)
                            .withString(Constants.ROUTER_STRING, code)
                            .withBoolean(Constants.ROUTER_BOOLEAN, true)
                            .navigation();
                } else if (code.startsWith(Constants.QRCODE_PREFIX_TYPE.USER)) {
                    ARouter.getInstance().build(RouterHub.APP_CONTACTDETAILSACTIVITY)
                            .withString(Intent.EXTRA_TITLE, Constants.CONTACT_DETAILS)
                            .withString(Constants.ROUTER_STRING, code)
                            .navigation();
                }
            }
        }
    }
}
